import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/event/refresh_home_page_event.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/utils/event_bus_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/payment_confirm_dialog.dart';

class CompanySettlementController extends GetxController {
  FocusNode remarksFocusNode = FocusNode();
  FocusNode paymentAccountFocusNode = FocusNode();

  //收款账户id
  String receivedAccountId = "";

  //收款账户编号
  String receivedAccountCode = "";

  //收款账户名称
  String receivedAccountName = "";

  //付款账户id
  String paymentAccountId = "";

  //付款账户名称
  String paymentAccountName = "";

  //付款金额
  double paymentAmount = 0.0;

  //可用余额
  double companyAmount = 0.0;

  //备注
  String remarks = "";

  @override
  void onReady() {
    super.onReady();
    dynamic arguments = Get.arguments;
    if (arguments != null && arguments is Map) {
      paymentAccountId = arguments[Constant.PARAM_COMPANY_ID];
      paymentAccountName = arguments[Constant.PARAM_COMPANY_NAME];
      companyAmount = arguments[Constant.PARAM_COMPANY_AMOUNT];
      update();
    }
  }

  void setRemarks(String value) {
    remarks = value;
  }

  void setPaymentAmount(String value) {
    if (value.isEmpty) {
      paymentAmount = 0.0;
    } else {
      paymentAmount = double.parse(value);
    }
    update();
  }

  Future<void> addAccount() async {
    clearFocus();
    dynamic data = await Get.toNamed(Routes.addAccount, arguments: paymentAccountId);
    if (data != null && data is Map) {
      receivedAccountId = data[Constant.PARAM_COMPANY_ID];
      receivedAccountName = data[Constant.PARAM_COMPANY_NAME];
      receivedAccountCode = data[Constant.PARAM_COMPANY_CODE];
      update();
    }
  }

  Future<void> selectAccount() async {
    clearFocus();
    dynamic data = await Get.toNamed(Routes.selectAccount, arguments: paymentAccountId);
    if (data != null && data is Map) {
      receivedAccountId = data[Constant.PARAM_COMPANY_ID];
      receivedAccountName = data[Constant.PARAM_COMPANY_NAME];
      receivedAccountCode = data[Constant.PARAM_COMPANY_CODE];
      update();
    }
  }

  Future<void> accountManage() async {
    clearFocus();
    dynamic data = await Get.toNamed(Routes.accountManage, arguments: paymentAccountId);
    if (data != null && data is Map) {
      receivedAccountId = data[Constant.PARAM_COMPANY_ID];
      receivedAccountName = data[Constant.PARAM_COMPANY_NAME];
      receivedAccountCode = data[Constant.PARAM_COMPANY_CODE];
      update();
    }
  }

  void confirm() {
    if (receivedAccountId.isEmpty || paymentAmount <= 0.0) {
      return;
    }
    if (receivedAccountId == paymentAccountId) {
      ToastUtils.toast("收款账户不能和付款账户一样");
      return;
    }
    if (companyAmount <= 0.0) {
      ToastUtils.toast("没有可用余额");
    }
    if (companyAmount < paymentAmount) {
      ToastUtils.toast("付款金额应小于等于可用余额");
      return;
    }

    clearFocus();

    showPaymentConfirmDialog();
  }

  void showPaymentConfirmDialog() {
    showDialog(
      barrierDismissible: false,
      context: Get.context!,
      builder: (BuildContext context) {
        return PaymentConfirmDialog(
          paymentAccountId: paymentAccountId,
          receivedAccountName: receivedAccountName,
          receivedAccountCode: receivedAccountCode,
          paymentAmount: paymentAmount,
          onValueCallback: (value) {
            companySettlement(value);
          },
        );
      },
    );
  }

  void companySettlement(String value) {
    httpManager.post(
      Get.context,
      HttpAddress.COMPANY_SETTLEMENT,
      {
        "superId": paymentAccountId,
        "targetId": receivedAccountId,
        "amount": paymentAmount,
        "desc": remarks,
        "code": value,
      },
      isToast: true,
      isLoadingDialog: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          return;
        }
        companyAmount = companyAmount - paymentAmount;
        update();
        EventBusUtils.fire(RefreshHomePageEvent());
        goToPaymentResultPage(data.toString());
      },
    );
  }

  Future<void> goToPaymentResultPage(String id) async {
    dynamic data = await Get.toNamed(Routes.paymentResult, arguments: id);
    if (data != null) {
      Get.back();
    }
  }

  void clearFocus(){
    paymentAccountFocusNode.unfocus();
    remarksFocusNode.unfocus();
  }
}
